package com.gjy.util.captcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.Properties;
import java.util.UUID;

import static com.google.code.kaptcha.Constants.*;

/**
 * 验证码生成器
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-10-23 08:54:40
 */
public class CaptchaConfig {
    private static final Logger log = LoggerFactory.getLogger(CaptchaConfig.class);

    private DefaultKaptcha getCaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 是否有边框 默认为true 我们可以自己设置yes，no
        properties.setProperty(KAPTCHA_BORDER, "yes");
        // 验证码文本字符颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
        // 验证码图片宽度 默认为200
        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
        // 验证码图片高度 默认为50
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
        // 验证码文本字符大小 默认为40
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
        // KAPTCHA_SESSION_KEY
        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
        // 验证码文本字符长度 默认为5
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    private DefaultKaptcha getCaptchaMath() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 是否有边框 默认为true 我们可以自己设置yes，no
        properties.setProperty(KAPTCHA_BORDER, "yes");
        // 边框颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
        // 验证码文本字符颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
        // 验证码图片宽度 默认为200
        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
        // 验证码图片高度 默认为50
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
        // 验证码文本字符大小 默认为40
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
        // KAPTCHA_SESSION_KEY
        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
        // 验证码文本生成器
        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.gjy.util.captcha.CaptchaTextCreator");
        // 验证码文本字符间距 默认为2
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
        // 验证码文本字符长度 默认为5
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
        // 验证码噪点颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
        // 干扰实现类
        properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    public CaptchaResponse getCode() {
        CaptchaResponse response = new CaptchaResponse();
        String uuid = UUID.randomUUID().toString();
        String base64;
        try (ByteArrayOutputStream bs = new ByteArrayOutputStream()) {
            DefaultKaptcha captcha = getCaptcha();
            String text = captcha.createText();
            log.info("text = {}", text);
            // redis.setExpire(uuid, text, 300); // 以UUID为键,单词为值, 放进redis中
            BufferedImage captchaImage = captcha.createImage(text);
            ImageIO.write(captchaImage, "jpg", bs);
            base64 = Base64.getEncoder().encodeToString(bs.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        response.setUuid(uuid);
        response.setBase64(base64);
        return response;
    }

    public CaptchaResponse getMath() {
        CaptchaResponse response = new CaptchaResponse();
        String uuid = UUID.randomUUID().toString();
        String base64;
        try (ByteArrayOutputStream bs = new ByteArrayOutputStream()) {
            DefaultKaptcha captcha = getCaptchaMath();
            String text = captcha.createText();
            log.info("text = {}", text);
            String capText = text.substring(0, text.lastIndexOf("@"));
            String codeText = text.substring(text.lastIndexOf("@") + 1);
            log.info("capText = {}, codeText = {}", capText, codeText);
            // redis.setExpire(uuid, codeText, 300); // 以UUID为键,单词为值, 放进redis中
            BufferedImage captchaImage = captcha.createImage(capText);
            ImageIO.write(captchaImage, "jpg", bs);
            base64 = Base64.getEncoder().encodeToString(bs.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        response.setUuid(uuid);
        response.setBase64(base64);
        return response;
    }

}
